require 'libglade2'

require 'settingsdialog'
require 'adddialog'
require 'link'
require 'linkmanager'

class Rsgtkd 
  attr :glade
  attr :window
  attr :settings
  attr :linkmanager
  
  def initialize(path_or_data, root = nil, domain = nil, localedir = nil, flag = GladeXML::FILE)
    # initialize gui 
    @glade = GladeXML.new(path_or_data, root, domain, localedir, flag) { |handler| method(handler) }
    @window = @glade['window']
    
    # load settings
    @settings = Settings.load
    
    # init linkManager
    @linkmanager = LinkManager.new @settings
    # add treeview to main panel
    @glade['treeview_container'].add @linkmanager.treeview
  end
  
  def on_window_delete_event
    Gtk.main_quit
  end	
  
  def on_toolbar_clear_clicked
    @linkmanager.clear
  end
  
  def on_toolbar_add_clicked
    dialog = AddDialog.new(self)
  end
  
  def on_toolbar_download_clicked
    
    @linkmanager.download_all 
  end
  
  def on_toolbar_settings_clicked
    dialog = SettingsDialog.new self
  end
  
  def on_settings_changed(settings) 
    @settings = settings
  end
  
  def on_toolbar_about_clicked
    dialog = Gtk::AboutDialog.new
    dialog.name = "Rapidshare GTK downloader"
    dialog.version = "0.1"
    dialog.copyright = "(C) 2009 Steven Van Bael"
    dialog.comments = "GTK tool for downloading rapidshare links"
    dialog.authors = ["Steven Van Bael"]
    
    dialog.run
    dialog.destroy
  end
  
  def on_toolbar_cancel_clicked 
    @linkmanager.cancel
  end
  
  def add_links_from_text(text)
    text.each { |line|
        @linkmanager.add_link line
    }
  end
end

#main
if __FILE__ == $0
  GLADE_FILE = "gui.glade"
  APP_NAME = "Rapidshare GTK downloader"
  g = Rsgtkd.new(GLADE_FILE, nil, APP_NAME)
  window = g.glade['window']
  window.show_all
  Gtk.main
end
